package com.huluobo.a2201mvvm_imdemo.mine;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;


import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2201mvvm_imdemo.R;
import com.huluobo.a2201mvvm_imdemo.databinding.FragmentMineBinding;
import com.huluobo.a2201mvvm_imdemo.login.LoginActivity;
import com.huluobo.a2201mvvm_imdemo.mvvm.BaseMVVMFragment;
import com.hyphenate.chat.EMClient;
import com.uuzuche.lib_zxing.activity.CodeUtils;

public class MineFragment extends BaseMVVMFragment<FragmentMineBinding, MineViewModel> {
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        viewModel.logoutSuccess.observe(this, s -> {
            ToastUtils.showLong(s);
            Intent intent = new Intent(requireContext(), LoginActivity.class);
            startActivity(intent);
            requireActivity().finish();
        });

        viewModel.logoutFailed.observe(this, ToastUtils::showLong);

    }

    @Override
    protected void initView() {
        dataBinding.mineLogout.setOnClickListener(v -> {
            viewModel.logout();
        });

        //getString(R.string.logout) 拿到String这个xml中对应id的String ,当前这里是   退出(%s)登录
        //EMClient.getInstance().getCurrentUser() 拿到当前登录的用户, 当前这里是 dd1
        //String.format 会找到第一个参数中带%s的位置,将第二个参数替换到对应的位置
        String username = String.format(getString(R.string.logout), EMClient.getInstance().getCurrentUser());
        dataBinding.mineLogout.setText(username);

        String QRCodeName = EMClient.getInstance().getCurrentUser();
        Bitmap logo = BitmapFactory.decodeResource(getResources(), R.drawable.d);
        Bitmap image = CodeUtils.createImage(QRCodeName, 400, 400, logo);
        dataBinding.mineQRCode.setImageBitmap(image);
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}